#!/usr/bin/python

"""Project Euler Solution 012

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.numbers.advanced_math import number_of_divisors
from euler.numbers.primes import Primes
from euler.numbers.number_theory import TriangleNumbers 

def get_answer():
    """Question:
    
    The sequence of triangle numbers is generated by adding the natural 
    numbers. So the 7th triangle number would be 
    1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
    
    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
    
    Let us list the factors of the first seven triangle numbers:
    
         1: 1
         3: 1,3
         6: 1,2,3,6
        10: 1,2,5,10
        15: 1,3,5,15
        21: 1,3,7,21
        28: 1,2,4,7,14,28
    
    We can see that 28 is the first triangle number to have over 
    five divisors.
    
    What is the value of the first triangle number to have over 
    five hundred divisors?
    """
    
    #Initialise prime cache
    primes = Primes()
    
    #Return result
    return next(
               triangle_number for triangle_number in TriangleNumbers() 
                if number_of_divisors(triangle_number, primes) > 500
            )


if __name__ == "__main__":
    cProfile.run("print(get_answer())")
